<?php

namespace Home\Controller;

class SendcardcodeController{

    function sendCardCodeEmailBySAP()
    {
        // OA HOST/sapSupplier/sendCardCodeEmailBySAP/card_code/C20101,C20102
        $cardCodeString = I('card_code');
        //var_dump($cardCodeString);die();
        $cardCodeArray = explode(',',$cardCodeString);
        $sqlString = '';
        $tmpString = '';
        foreach ($cardCodeArray as $key => $item){
            $tmpString .= "'".$item."',";
        }
        $sqlString = substr($tmpString,0,strlen($tmpString)-1);
        $sqlString = '('.$sqlString.')';
        //var_dump($sqlString);die();

        // send out email
        $platformModel = M('Platform');
        $result = $platformModel->where("card_code in {$sqlString}")->select();
        //var_dump($result);die();

        $connent = "        
        <table width='100%' align='center' border='1px' cellpadding='0px' cellspacing='0px' style='border-top: 0px solid #000000; border-left: 0px solid #000000; border-right: 0px solid #000000; border-bottom: 0px solid #000000;'>
        <tr style='height: 30px'>
            <td nowrap=\"nowrap\" align='center'>组长</td>
            <td nowrap=\"nowrap\" align='center'>经理/主管</td>
            <td nowrap=\"nowrap\" align='center'>专员</td>
            <td nowrap=\"nowrap\" align='center'>助理</td>
            <td nowrap=\"nowrap\" align='center'>性质</td>
            <td nowrap=\"nowrap\" align='center'>客户</td>
            
            <td nowrap=\"nowrap\" align='center'>CardCode</td>
            <td nowrap=\"nowrap\" align='center'>原命名</td>
            <td nowrap=\"nowrap\" align='center'>品牌数</td>
            <td nowrap=\"nowrap\" align='center'>品类数</td>
            <td nowrap=\"nowrap\" align='center'>SKU数</td>
            <td nowrap=\"nowrap\" align='center'>上线状态</td>
            
            <td nowrap=\"nowrap\" align='center'>备注</td>
        </tr>
        ";

        foreach ($result as $key => $data) {

            $connent .= "<tr style='height: 50px'>
            <td align='center'>" . $data['team_leader'] . "</td>
            <td align='center'>" . $data['team_manager'] . "</td>
            <td align='center'>" . $data['team_commissioner'] . "</td>
            <td align='center'>" . $data['team_assistant'] . "</td>
            <td align='center'>" . $this->getPlatformCNType($data['p_type']) . "</td>
            <td align='center'>" . $data['p_name'] . "</td>
            
            <td align='center'>" . $data['card_code'] . "</td>
            <td align='center'>" . $data['original_name'] . "</td>
            <td align='center'>" . $data['brand_count'] . "</td>
            <td align='center'>" . $data['category_count'] . "</td>
            <td align='center'>" . $data['sku_count'] . "</td>
            <td align='center'>" . $data['online_state'] . "</td>
            
            <td align='center'>" . $data['p_remark'] . "</td>
        </tr>";

        }

        $connent .= "</table>";

        if($result) {
            //TODO: 发送邮件
            // 市场部全体，抄送给Tony和wendy.:
            $receiver = "mktd@g-town.com.cn";
            $ccArray = array('clientservice@g-town.com.cn','wendy.li@g-town.com.cn','molly.li@g-town.com.cn','maxwell.shi@g-town.com.cn','austin@g-town.com.cn','tony@g-town.com.cn');
            //$receiver = "maxwell.shi@g-town.com.cn";
            //$ccArray = array('maxwell.shi@g-town.com.cn','maxwell.shi@g-town.com.cn');
            $this->sendEmail($receiver, "新平台：" . $cardCodeString, $connent, '', $ccArray);
        }
        exit;
    }

    function getPlatformCNType($type)
    {
        $cnType = '';
        switch ($type){
            case 1:
                $cnType = '积分';
                break;
            case 2:
                $cnType = '分期';
                break;
            case 4:
                $cnType = '集采';
                break;
            default:
                break;
        }
        return $cnType;

    }

    /**
     * 发送模版邮件
     * @param unknown $tomail
     * @param unknown $title
     * @param unknown $connent
     * @param unknown $attachment
     * @param unknown $cc
     */
    protected  function sendEmail($tomail,$title,$connent,$attachment=array(),$cc=array()){
        //邮件模版    内容替换%connent%
        $mailmodel =<<<html
<div id="mailContentContainer"
	style="font-size: 14px; padding: 0px; height: auto; min-height: auto; font-family: &amp; quot; lucida Grande&amp;quot; , Verdana; margin-right: 0px;">
	<div style="background: #ededed; padding-top: 52px; width: 100%;">
		<div class="wrapper"
			style="background: #fff; width: auto; max-width: 80%; margin: auto; border: 10px solid #ededed;">
			<div class="container content">
				<!--<div class="row" style="background: #3498db; height: 10px;">&nbsp;</div>-->
				<!--<div class="row" style="text-align: left; padding-left: 10px; padding-bottom: 10px; border-bottom: 1px solid #f4f3f3; margin-bottom: 20px;"></div>-->
				<div class="row">
					<div style="position: relative; padding: 5px 5px 5px 5px;">%connent%</div>
				</div>
			</div>
			<div style="background: #f3f3f3; width: 100%; color: #999; padding-top: 10px; padding-bottom: 25px; font-size: 12px;">
				本邮件为系统发出，请勿回复  | 
			</div>
		</div>
	</div>
	<br>
	<br>
	<style></style>
</div>
html;
        $connent = str_replace("%connent%", $connent ,$mailmodel);
        return sendEamilByOA($tomail, $title, $connent,$attachment,$cc);
    }

}